package com.leyou.auth.config;


import com.leyou.auth.client.AuthClient;
import com.leyou.auth.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@EnableConfigurationProperties(ClientProperties.class)
@ConditionalOnProperty(prefix = "ly.auth", name = {"clientId", "secret"})
public class AuthConfiguration {

    @Autowired
    private AuthClient authClient;

    @Bean
    public JwtUtils jwtUtils() throws InterruptedException {
        //循环
        while (true) {
            try {
                //掉方法获取key
                String key = authClient.getKey("user-service", "1234");
                //获取jwtUtils
                JwtUtils jwtUtils = new JwtUtils(key);
                log.info("秘钥加载成功");
                return jwtUtils;
            } catch (Exception e) {
                log.info("秘钥加载失败，十秒后重试，失败原因："+e);
                Thread.sleep(10000);
            }
        }
    }
}
